#pragma once

#include "../imgui/imgui.h"

#include "../Gut/Mesh.h"
#include "../Gut/Texture.h"
#include "../Gut/RenderDesc.h"

struct GutEditor
{
	inline bool MouseInWindow()
	{
		ImGuiIO &io = ImGui::GetIO();

		ImVec2 mousePos = io.MousePos;
		ImVec2 pos = ImGui::GetWindowPos();
		ImVec2 size = ImGui::GetWindowSize();

		if (mousePos.x > pos.x && mousePos.x < pos.x + size.x &&
			mousePos.y > pos.y && mousePos.y < pos.y + size.y)
		{
			return true;
		}
		return false;
	}
	inline void Image(const unsigned int &t, ImVec2 sz = {80, 80}, ImVec2 uv0 = {0, 0}, ImVec2 uv1 = {1, 1})
	{
		ImGui::Image((ImTextureID)t, sz, uv0, uv1);
	}

	inline void Framebuffer(const Gut::Texture2D &t, ImVec2 sz = {80, 80}, ImVec2 uv0 = {0, 1}, ImVec2 uv1 = {1, 0})
	{
		ImGui::Image((ImTextureID)t.ID, sz, uv0, uv1);
	}
	inline void Framebuffer(const ImTextureID &t, ImVec2 sz = {80, 80}, ImVec2 uv0 = {0, 1}, ImVec2 uv1 = {1, 0})
	{
		ImGui::Image(t, sz, uv0, uv1);
	}

	inline void AddFramebufferScreen(const Gut::Texture2D &t, ImVec2 wsz = {80, 80}, ImVec2 uv0 = {0, 1}, ImVec2 uv1 = {1, 0})
	{
		ImDrawList *drawList = ImGui::GetWindowDrawList();
		auto wPos = ImVec2(0, 0);

		drawList->AddImage((ImTextureID)t.ID, wPos, ImVec2(wPos.x + wsz.x, wPos.y + wsz.y), uv0, uv1);
	}

	inline void AddFramebuffer(const Gut::Texture2D& tex, ImVec2 sz = {50, 50}, ImVec2 uv0 = {0, 1}, ImVec2 uv1 = {1, 0})
	{
		ImDrawList *drawList = ImGui::GetWindowDrawList();
		auto wPos = ImGui::GetWindowPos();
		auto wsz = ImGui::GetWindowSize();

		drawList->AddImage((ImTextureID)tex.ID, wPos, ImVec2(wPos.x + wsz.x, wPos.y + wsz.y), uv0, uv1);
	}

	inline void AddFramebuffer(const ImTextureID tID, ImVec2 uv0 = {0, 1}, ImVec2 uv1 = {1, 0})
	{
		ImDrawList *drawList = ImGui::GetWindowDrawList();
		auto wPos = ImGui::GetWindowPos();
		auto wsz = ImGui::GetWindowSize();
		drawList->AddImage((ImTextureID)tID, wPos, ImVec2(wPos.x + wsz.x, wPos.y + wsz.y), uv0, uv1);
	}

	inline bool MousePos(float &xa, float &ya)
	{
		ImGuiIO &io = ImGui::GetIO();
		ImVec2 mousePos = io.MousePos;

		ImVec2 windowPos = ImGui::GetWindowPos();
		ImVec2 windowSize = ImGui::GetWindowSize();
		xa = (float)(mousePos.x - windowPos.x) / (float)windowSize.x;
		ya = (float)(mousePos.y - windowPos.y) / (float)windowSize.y;

		return xa > 0.0f && xa < 1.0f && xa > .0f && xa < 1.0f;
	}

	inline void Edit(const std::string &label, Gut::Texture2D &t)
	{
		ImGui::Text(label.c_str());
		ImGui::SameLine();
		ImGui::Text(t.m_Path.c_str());
		Image(t.ID);
	}
};
